package com.kungfuhacking.wristbandpro.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.InputView;
import com.kungfuhacking.wristbandpro.login.presenter.ILoginPresenter;
import com.kungfuhacking.wristbandpro.login.view.ILoginView;
import com.kungfuhacking.wristbandpro.utils.Constant;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class LoginActivity extends BaseActivity implements ILoginView, View.OnClickListener {

    private String TAG = getClass().getSimpleName();

//    private CompositeDisposable compositeDisposable = new CompositeDisposable();
//    compositeDisposable.clear(); //切断所有水管

    private ILoginPresenter iLoginPresenter = new ILoginPresenter(this);
    private InputView ivAccount;
    private InputView ivPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);
        initView();
        EventBus.getDefault().register(this);

//        if (bundle == null)
//            iLoginPresenter.autoLogin();
//        else if (bundle.getBoolean("autoLogin", true)) {
//            iLoginPresenter.autoLogin();
//        } else {
//            ivAccount.setTxt(sp.getString(Constant.MOBILE, ""));
//            ivPassword.setTxt("");
//        }

        iLoginPresenter.autoLogin();

    }

    private void initView() {

        TextView btnforgetpwd = (TextView) findViewById(R.id.btn_forget_pwd);
        TextView btnregister = (TextView) findViewById(R.id.btn_register);
        TextView btnlogin = (TextView) findViewById(R.id.btn_login);

        ivAccount = (InputView) findViewById(R.id.ivAccount);
        ivPassword = (InputView) findViewById(R.id.ivPassword);

        ivAccount.setInputType(InputType.TYPE_CLASS_PHONE);
        ivPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        btnlogin.setOnClickListener(this);
        btnregister.setOnClickListener(this);
        btnforgetpwd.setOnClickListener(this);
    }

    //接受到新消息
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onDataSynEvent(String message) {

        if (Constant.EXIT_APP.equals(message)) {
            finish();
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG, " === onNewIntent ===");
        if (intent != null && intent.getBooleanExtra("exit", false)) {
            ivAccount.setTxt(sp.getString(Constant.MOBILE, ""));
            ivPassword.setTxt("");
        }

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btn_login://登录
                iLoginPresenter.login(
                        ivAccount.getText(), ivPassword.getText());
                break;

            case R.id.btn_register://注册
                nextActivity(RegisterActivity.class);
                break;

            case R.id.btn_forget_pwd://忘记密码
                nextActivity(ForgetActivity.class);
                break;

        }

    }

    @Override
    public String getUsername() {
        return ivAccount.getText();
    }

    @Override
    public String getPassword() {
        return ivPassword.getText();
    }

    @Override
    public void setUsername(String username) {
        ivAccount.setTxt(username);
    }

    @Override
    public void setPassword(String password) {
        ivPassword.setTxt(password);
    }

    @Override
    public String getCacheUsername() {
        return sp.getString(Constant.MOBILE, "");
    }

    @Override
    public String getCachePassword() {
        return sp.getString(Constant.PASSWORD, "");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iLoginPresenter.clearRequest();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        System.exit(0);
    }

}
